﻿using System;
using System.Windows;

namespace MusicPlayer
{
    class InputHandlerMouse : ICoreCommands, IPlayListCommands
    {
        public void HandleCommand(Commands command, int playerIndex)
        {
            switch (command)
            {
                case Commands.Play:
                    {
                        try
                        {
                            BaseCoreArgs args = new BaseCoreArgs(playerIndex);
                            OnPlay(this, args);
                        }
                        catch (CoreMediaFailureException cmfe)
                        {
                            MessageBox.Show(String.Format(
                                "Player {0} reported media loading failure.\nDetails: \"{1}\"", cmfe.SourcePlayer, cmfe.Message));
                        }
                        break;
                    }
                case Commands.Pause:
                    {
                        BaseCoreArgs args = new BaseCoreArgs(playerIndex);
                        OnPause(this, args);
                        break;
                    }
                case Commands.Stop:
                    {
                        BaseCoreArgs args = new BaseCoreArgs(playerIndex);
                        OnStop(this, args);
                        break;
                    }
                case Commands.Next:
                    {
                        try
                        {
                            BaseCoreArgs args = new BaseCoreArgs(playerIndex);
                            OnNext(this, args);
                        }
                        catch (CoreMediaFailureException cmfe)
                        {
                            MessageBox.Show(String.Format(
                                "Player {0} reported media loading failure.\nDetails: \"{1}\"", cmfe.SourcePlayer, cmfe.Message));
                        }
                        break;
                    }
                case Commands.Previous:
                    {
                        try
                        {
                            BaseCoreArgs args = new BaseCoreArgs(playerIndex);
                            OnPrevious(this, args);
                        }
                        catch (CoreMediaFailureException cmfe)
                        {
                            MessageBox.Show(String.Format(
                                "Player {0} reported media loading failure.\nDetails: \"{1}\"", cmfe.SourcePlayer, cmfe.Message));
                        }
                        break;
                    }
                case Commands.Mute:
                    {
                        BaseCoreArgs args = new BaseCoreArgs(playerIndex);
                        OnMute(this, args);
                        break;
                    }
                default:
                    {
                        throw new ApplicationException("Unknown command");
                    }
            }
        }

        public void ChangeVolume(double newVolume, int playerIndex)
        {
            VolumeChangeEventArgs args = new VolumeChangeEventArgs(playerIndex, newVolume);
            OnVolumeChange(this, args);
        }

        public void OrderChange(string newOrder)
        {
            PlayListOrderArgs args = new PlayListOrderArgs(newOrder);
            OnOrderChange(this, args);
        }

        public void CurrentSelectionChange(int currentIndex)
        {
            CurrentSongArgs args = new CurrentSongArgs(currentIndex);
            OnCurrentSongChange(this, args);
        }

        public void AddFiles()
        {
            OnAddFiles(this, new EventArgs());
        }

        public void ClearPlayList()
        {
            OnClear(this, new EventArgs());
        }

        public void SortPlayList()
        {
            OnSortFiles(this, new EventArgs());
        }

        //Core specific events
        public event EventHandler<BaseCoreArgs> OnPlay;

        public event EventHandler<BaseCoreArgs> OnPause;

        public event EventHandler<BaseCoreArgs> OnStop;

        public event EventHandler<BaseCoreArgs> OnPrevious;

        public event EventHandler<BaseCoreArgs> OnNext;

        public event EventHandler<VolumeChangeEventArgs> OnVolumeChange;

        public event EventHandler<BaseCoreArgs> OnMute;

        //public event EventHandler<PlayerChangeArgs> OnPlayerChange;

        public event EventHandler<DriverChangeArgs> OnDriverChange;

        //PlayList specific events
        public event EventHandler OnAddFiles;

        public event EventHandler OnSortFiles;

        public event EventHandler OnClear;

        public event EventHandler<PlayListOrderArgs> OnOrderChange;

        public event EventHandler<CurrentSongArgs> OnCurrentSongChange;
    }
}
